<?php


namespace app\api\controller;

use app\admin\model\Admin;
use app\api\model\InfoTypeRecord;
use app\api\model\UserMonitorModel;
use app\api\model\UserTypeBluetooth;
use app\common\library\Sms;

class Test
{
    public function sendMsg()
    {
        $mobile_str = 18838186778;
        $template = [
            'name' => '大古奥特曼',
        ];
        $template['target'] = '温度';
        Sms::notice($mobile_str, $template, 'SMS_227256586');
    }

    function single_process_data()
    {
    	$item = Db("info_type_timer")->where("is_use",0)->find();
    	 Db("info_type_timer")->where("id",$item['id'])->update(['is_use' => 1]);

        $userBluetoothModel = new UserTypeBluetooth();
        $userMonitor = new UserMonitorModel();

        $type = $item['type'];
        $str = $item['content'];
        $time = $item['time'];
        $user_id = $item['user_id'];

        $user = Db("user")->where(['id' => $user_id])->find();

        //当前用户手机号和管家手机号
        $gunajia_mobile = Admin::where('find_in_set(:user_ids,user_ids)', ['user_ids' => $user_id])->cache(true, 300)->where('mobile', 'neq', '')->column('mobile');
        $user_mobile = $user['mobile'];

        array_push($gunajia_mobile, $user_mobile);

        $gunajia_mobile = array_unique($gunajia_mobile);

        $mobile_str = implode($gunajia_mobile, ',');

        $template = [
            'name' => $user['nickname'],
        ];

        //获取温度历史数据
        if ($type == 1) {
            //$str = 'AC D6 02 01 E5 07 09 02 04 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 02 0E 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 24 0E 00 00 25 0E 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 1E 0E 00 00 16 0E 17 0E 00 00 00 00 1A 0E 1B 0E 00 00 00 00 0A 0E 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 0E 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 7D 0D 23 0E 39 0E 50 0E 2A 0E 46 0E 47 0E 41 0E 46 0E 4D 0E 51 0E 42 0E 43 0E 15 0E 1D 0E 1E 0E 1C 0E 1A 0E 18 0E 15 0E 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 7E';
            $str_arr = explode(" ", $str);

            if(count($str_arr) >= 8){
            	$hot_str = $str_arr[7] . $str_arr[6];
	            $hot = base_convert($hot_str, 16, 10) / 100;
	            if ($user['sms_notify'] == 1) {
	                $blue = UserTypeBluetooth::get(['pid' => 23]);
	                if (!empty($blue)) {
	                    $user_blue = $userMonitor::get(['user_id' => $user_id, 'type_id' => $blue['id']]);
	                    $b_num = !empty($user_blue) ? $user_blue : $blue;
	                    if (!empty($b_num)) {
	                        if ($hot > $b_num['high'] || $hot < $b_num['low']) {
	                            $template['target'] = '温度';
	                            $status = Db("user_type_bluetooth")->where("id",1)->value("status");
	                            if($status && $user['sms_notify'] == 1){
	                                log_print('短信', $template['target']);
	                                Sms::notice($mobile_str, $template, 'SMS_227256586');
	                            } 
	                        }
	                    }
	                }

	            }

	            //温度
	            $user_info = InfoTypeRecord::get(['user_id' => $user['id'], 'type_id' => 23, 'day' => $time]);

	            if (!$user_info) {
	                $user_info = new InfoTypeRecord();
	                $user_info->save(['user_id' => $user['id'], 'type_id' => 23, 'day' => $time, 'type_value' => $hot, 'is_use' => 1]);
	            } else {
	                $user_info->save(['type_value' => $hot]);
	            }
            }
            
        } elseif ($type == 2) {
            //$str = 'A0 26 01 01 E5 07 08 1F 0A 00 00 00 00 00 00 00 F1 00 F2 00 F2 00 00 00 00 00 00 00 F1 00 F2 00 F2 00 F3 00 F3 00 F3 00 F3 00 F3 00 F3 00 F3 00 F3 00 F3 00 F3 00 F3 00 F3 00 F3 00 F3 00 F3 00 F3 00 F3 00 F3 00 F3 00 F3 00 F3 00 F3 00 F3 00 00 00 00 00 00 00 F1 00 F2 00 F2 00 00 00 00 00 00 00 00 00 00 00 00 00 F1 00 F2 00 F2 00 F3 00 F3 00 F2 00 F2 00 F3 00 F2 00 F3 00 F4 00 F3 00 F4 00 F4 17 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 5A 00 64 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 4B 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 C4';

            $str_arr = explode(" ", $str);
            //获取计步历史数据(以天为单位返回)
            if ($str_arr[3] == '01') {
                if (isset($str_arr[4]) && isset($str_arr[5]) && isset($str_arr[6]) && isset($str_arr[7])) {
                    $year_x = $str_arr[5] . $str_arr[4];
                    $year = base_convert($year_x, 16, 10);
                    $month = base_convert($str_arr[6], 16, 10);
                    $day = base_convert($str_arr[7], 16, 10);
                    $time = $year . '-' . $month . '-' . $day;
                }

                $timex = strtotime($time);
                $num = 9;
                $arr = [];
                $arr_text = [];

                $check_bushu = 0;
                $check_kaluli = 0;
                $check_juli = 0;
//                $qian = 0;$shen = 0;
                for ($i = 0; $i < 144; $i++) {
                    $bu_shu = 0;  //步数

                    //for ($i = 0; $i < 1440; $i++) {

                    if (isset($str_arr[$num]) && $str_arr[$num + 1]) {
                        $i1_arr = str_split($str_arr[$num]);

                        $i2_arr = str_split($str_arr[$num + 1]);

                        $arr[$i] = [$i1_arr[0], $i1_arr[1], $i2_arr[0], $i2_arr[1]];
                    }

                    $arr_text[$i]['time'] = date('Y-m-d H:i:s', $timex + (600 * $i));
                    //$arr_text[$i]['time'] = date('Y-m-d H:i:s',$timex + (60 * $i));
//                    var_dump($arr[$i]);
                    if (isset($arr[$i][2])) {
                        if ($arr[$i][2] == '0') {
                            //走路 type == 17   都算步数
                            $arr_text[$i]['type'] = '走路';
                            $arr_text[$i]['num'] = base_convert($arr[$i][3] . $arr[$i][0] . $arr[$i][1], 16, 10);
                            $bu_shu += $arr_text[$i]['num'];  //加上所有的步数
                        } elseif ($arr[$i][2] == '1') {
                            // 跑步  type  == 17   都算步数
                            $arr_text[$i]['type'] = '跑步';
                            $arr_text[$i]['num'] = base_convert($arr[$i][3] . $arr[$i][0] . $arr[$i][1], 16, 10);
                            $bu_shu += $arr_text[$i]['num'];   //加上所有的步数
                        } elseif ($arr[$i][2] == '2') {
                            // 慢跑   type == 17   都算步数
                            $arr_text[$i]['type'] = '慢跑';
                            $arr_text[$i]['num'] = base_convert($arr[$i][3] . $arr[$i][0] . $arr[$i][1], 16, 10);
                            $bu_shu += $arr_text[$i]['num'];   //加上所有的步数
                        } elseif ($arr[$i][2] == 'F') {
                            //睡眠
                            $arr_text[$i]['type'] = '睡眠';
                            switch ($arr[$i][3]) {
                                case '1':
                                    //开始睡觉  type == 14  睡觉时间
                                    $arr_text[$i]['num'] = '开始入睡';
                                    $user_info = InfoTypeRecord::get(['user_id' => $user['id'], 'type_id' => 14, 'day' => $time, 'taketime' => strtotime($arr_text[$i]['time'])]);
                                    //只加入第一个入睡时间
                                    if (!$user_info) {
                                        $user_info = new InfoTypeRecord();
                                        $user_info->save(['user_id' => $user['id'], 'type_id' => 14, 'day' => $time, 'type_value' => strtotime($arr_text[$i]['time']), 'is_use' => 1, 'taketime' => strtotime($arr_text[$i]['time'])]);
                                    } else {
                                        $user_info->save(['type_value' => strtotime($arr_text[$i]['time'])]);
                                    }

                                    break;
                                case '2':
                                    //浅睡  type == 18 单位分钟
                                    $arr_text[$i]['num'] = '浅睡';
                                    $user_info = InfoTypeRecord::get(['user_id' => $user['id'], 'type_id' => 18, 'day' => $time, 'taketime' => strtotime($arr_text[$i]['time'])]);
                                    if (!$user_info) {
                                        $user_info = new InfoTypeRecord();
                                        $user_info->save(['user_id' => $user['id'], 'type_id' => 18, 'day' => $time, 'type_value' => strtotime($arr_text[$i]['time']), 'is_use' => 1, 'taketime' => strtotime($arr_text[$i]['time'])]);
                                    } else {
                                        $user_info->save(['type_value' => strtotime($arr_text[$i]['time'])]);
                                    }
                                    break;
                                case '3':
                                    //深睡  type == 19  单位分钟
                                    $arr_text[$i]['num'] = '深睡';
                                    $user_info = InfoTypeRecord::get(['user_id' => $user['id'], 'type_id' => 19, 'day' => $time, 'taketime' => strtotime($arr_text[$i]['time'])]);
                                    if (!$user_info) {
                                        $user_info = new InfoTypeRecord();
                                        $user_info->save(['user_id' => $user['id'], 'type_id' => 19, 'day' => $time, 'type_value' => strtotime($arr_text[$i]['time']), 'is_use' => 1, 'taketime' => strtotime($arr_text[$i]['time'])]);
                                    } else {
                                        $user_info->save(['type_value' => strtotime($arr_text[$i]['time'])]);
                                    }
                                    break;
                                case '4':
                                    //清睡  type == 16  清醒时间
                                    $arr_text[$i]['num'] = '清醒';
                                    //$user_info = UserInfoModel::get(['user_id'=>$user['id'],'type_id'=>16,'day'=>$time])
                                    $user_info = InfoTypeRecord::get(['user_id' => $user['id'], 'type_id' => 16, 'day' => $time, 'is_use' => 1, 'taketime' => strtotime($arr_text[$i]['time'])]);
                                    if (!$user_info) {
                                        //$user_info = new UserInfoModel();
                                        //$user_info->save(['user_id'=>$user['id'],'type_id'=>16,'day'=>$time,'type_value'=>$arr_text[$i]['time'],'is_title'=>1]);
                                        $user_info = new InfoTypeRecord();
                                        $user_info->save(['user_id' => $user['id'], 'type_id' => 16, 'day' => $time, 'type_value' => strtotime($arr_text[$i]['time']), 'is_use' => 1, 'taketime' => strtotime($arr_text[$i]['time'])]);
                                    } else {
                                        $user_info->save(['type_value' => strtotime($arr_text[$i]['time'])]);
                                    }
                                    break;
                                case '5':
                                    //快速眼动睡眠
                                    $arr_text[$i]['num'] = '快速眼动睡眠';
                                    break;
                                default:
                                    $arr_text[$i]['num'] = '数据错误';
                                    break;
                            }
                        }
                    }

                    /*
                     * 步数统计
                     */
                    $bu_shu = $bu_shu > 0 ? $bu_shu : 0;
                    if ($user['sms_notify'] == 1) {
                        $blue = $userBluetoothModel::get(['pid' => 17]);
                        if (!empty($blue)) {
                            $user_blue = $userMonitor::get(['user_id' => $user_id, 'type_id' => $blue['id']]);
                            $check_bushu_data = !empty($user_blue) ? $user_blue : $blue;
                            if (!empty($check_bushu_data)) {
                                if ($bu_shu > $check_bushu_data['high'] || $bu_shu < $check_bushu_data['low']) {
                                    $check_bushu = 1;
                                }
                            }
                        }
                    }


                    $user_info = InfoTypeRecord::get(['user_id' => $user['id'], 'type_id' => 17, 'day' => $time, 'is_use' => 1, 'taketime' => strtotime($arr_text[$i]['time'])]);
                    if (!$user_info) {
                        $user_info = new InfoTypeRecord();
                        $user_info->save(['user_id' => $user['id'], 'type_id' => 17, 'day' => $time, 'type_value' => $bu_shu, 'is_use' => 1, 'taketime' => strtotime($arr_text[$i]['time'])]);
                    } else {
                        $user_info->save(['type_value' => $bu_shu]);
                    }
                    /*
                     * 卡路里统计
                     */
                    if ($bu_shu > 0) {
                        $weight = !empty($user['weight']) ? $user['weight'] : 120;

                        $bu_length = 75 * $weight * ($bu_shu * 0.78) / 100000 / 10;

                        $bu_length = number_format($bu_length, 2);

                    } else {
                        $bu_length = 0;
                    }

                    if ($user['sms_notify'] == 1) {
                        $blue = $userBluetoothModel::get(['pid' => 24]);
                        if (!empty($blue)) {
                            $user_blue = $userMonitor::get(['user_id' => $user_id, 'type_id' => $blue['id']]);
                            $check_bushu_data = !empty($user_blue) ? $user_blue : $blue;
                            if (!empty($check_bushu_data)) {
                                if ($bu_length > $check_bushu_data['high'] || $bu_length < $check_bushu_data['low']) {
                                    $check_kaluli = 1;
                                }
                            }
                        }
                    }
                    $user_info = InfoTypeRecord::get(['user_id' => $user['id'], 'type_id' => 24, 'day' => $time, 'is_use' => 1, 'taketime' => strtotime($arr_text[$i]['time'])]);
                    if (!$user_info) {
                        $user_info = new InfoTypeRecord();
                        $user_info->save(['user_id' => $user['id'], 'type_id' => 24, 'day' => $time, 'type_value' => $bu_length, 'is_use' => 1, 'taketime' => strtotime($arr_text[$i]['time'])]);
                    } else {
                        $user_info->save(['type_value' => $bu_length]);
                    }
                    /*
                     * 运动距离
                     */
                    if ($bu_shu > 0) {

                        $bu_length = 75 * $bu_shu / 100 / 1000;

                        $bu_limit = number_format($bu_length, 2);

                    } else {
                        $bu_limit = 0;
                    }

                   $blue = $userBluetoothModel::get(['pid' => 12]);
                    if (!empty($blue)) {
                        $user_blue = $userMonitor::get(['user_id' => $user_id, 'type_id' => $blue['id']]);
                        $check_bushu_data = !empty($user_blue) ? $user_blue : $blue;
                        if (!empty($check_bushu_data)) {
                            if ($bu_limit > $check_bushu_data['high'] || $bu_limit < $check_bushu_data['low']) {
                                $check_juli = 1;
                            }
                        }
                    }
                    $user_info = InfoTypeRecord::get(['user_id' => $user['id'], 'type_id' => 12, 'day' => $time, 'is_use' => 1, 'taketime' => strtotime($arr_text[$i]['time'])]);
                    if (!$user_info) {
                        $user_info = new InfoTypeRecord();
                        $user_info->save(['user_id' => $user['id'], 'type_id' => 12, 'day' => $time, 'type_value' => $bu_limit, 'is_use' => 1, 'taketime' => strtotime($arr_text[$i]['time'])]);
                    } else {
                        $user_info->save(['type_value' => $bu_limit]);
                    }

                    //类型和数据  加2
                    $num = $num + 2;
                    //$num = $num + 0.02;

//                    echo $arr_text[$i]['time'].':'.$arr_text[$i]['type'].'->'. $arr_text[$i]['num'] .'</br>';
                }
                $target = [];
                if ($check_bushu > 0) {
                    $status = Db("user_type_bluetooth")->where("id",6)->value("status");
                    if($status && $user['sms_notify'] == 1) $target[] = '步数';
//                    Sms::notice($mobile_str, $template, 'SMS_227256586');
                }
                if ($check_kaluli > 0) {
                    $status = Db("user_type_bluetooth")->where("id",7)->value("status");
                    if($status && $user['sms_notify'] == 1) $target[] = '卡路里';
//                    Sms::notice($mobile_str, $template, 'SMS_227256586');
                }
                if ($check_juli > 0) {
                    $status = Db("user_type_bluetooth")->where("id",8)->value("status");
                    if($status && $user['sms_notify'] == 1) $target[] = '运动距离';
//                    Sms::notice($mobile_str, $template, 'SMS_227256586');
                }
                //浅睡
                $qian_num = InfoTypeRecord::where(['user_id' => $user['id'], 'type_id' => 18, 'day' => $time])->count();
                $qian_num = $qian_num * 10;
                if ($user['sms_notify'] == 1) {
                    $blue = $userBluetoothModel::get(['pid' => 18]);
                    if (!empty($blue)) {
                        $user_blue = $userMonitor::get(['user_id' => $user_id, 'type_id' => $blue['id']]);
                        $b_num = !empty($user_blue) ? $user_blue : $blue;
                        if (!empty($b_num)) {
                            if ($qian_num > $b_num['high'] || $qian_num < $b_num['low']) {
                                $status = Db("user_type_bluetooth")->where("id",3)->value("status");
                                if($status && $user['sms_notify'] == 1)  $target[] = '浅睡';
//                                Sms::notice($mobile_str, $template, 'SMS_227256586');
                            }
                        }
                    }
                }
                //深睡
                $qian_num = InfoTypeRecord::where(['user_id' => $user['id'], 'type_id' => 19, 'day' => $time])->count();
                $qian_num = $qian_num * 10;
                if ($user['sms_notify'] == 1) {
                    $blue = $userBluetoothModel::get(['pid' => 19]);
                    if (!empty($blue)) {
                        $user_blue = $userMonitor::get(['user_id' => $user_id, 'type_id' => $blue['id']]);
                        $b_num = !empty($user_blue) ? $user_blue : $blue;
                        if (!empty($b_num)) {
                            if ($qian_num > $b_num['high'] || $qian_num < $b_num['low']) {
                                 $status = Db("user_type_bluetooth")->where("id",4)->value("status");
                                 if($status && $user['sms_notify'] == 1) $target[] = '深睡';
//                                Sms::notice($mobile_str, $template, 'SMS_227256586');
                            }
                        }
                    }
                }
                //清醒时间
                $qian_num = InfoTypeRecord::where(['user_id' => $user['id'], 'type_id' => 16, 'day' => $time])->count();
                $qian_num = $qian_num * 10;
                if ($user['sms_notify'] == 1) {
                    $blue = $userBluetoothModel::get(['pid' => 16]);
                    if (!empty($blue)) {
                        $user_blue = $userMonitor::get(['user_id' => $user_id, 'type_id' => $blue['id']]);
                        $b_num = !empty($user_blue) ? $user_blue : $blue;
                        if (!empty($b_num)) {
                            if ($qian_num > $b_num['high'] || $qian_num < $b_num['low']) {
                                 $status = Db("user_type_bluetooth")->where("id",5)->value("status");
                                 if($status && $user['sms_notify'] == 1) $target[] = '清醒时间';
                            }
                        }
                    }
                }

                $target_str = implode($target, ',');

                if (count($target) > 0 && !empty($target_str)) {
                    $template['target'] = $target_str;
                    log_print('短信', $target_str);
                    Sms::notice($mobile_str, $template, 'SMS_227256586');
                }
            }
        } elseif ($type == 3) {

            $str_arr = explode(" ", $str);
            if (isset($str_arr[4]) && isset($str_arr[5]) && isset($str_arr[6]) && isset($str_arr[7])) {
                $year_x = $str_arr[5] . $str_arr[4];
                $year = base_convert($year_x, 16, 10);
                $month = base_convert($str_arr[6], 16, 10);
                $day = base_convert($str_arr[7], 16, 10);
                $time = $year . '-' . $month . '-' . $day;
            }
            $timex = strtotime($time);
            $num = 9;
            $arr = [];
            $arr_text = [];
//            //心率  舒张压 收缩压  血氧的初始值 以及对应的次数
//            $xin = 0;$xin_num = 0;
//            $shu = 0;$shou = 0;$shu_num = 0;
//            $xue = 0;$xue_num = 0;
            $check_xinlv = 0;//6
            $check_gao = 0;//21
            $check_di = 0;//50
            $check_xueyang = 0;//7

            if ($user['sms_notify'] == 1) {
                $blue = $userBluetoothModel::get(['pid' => 6]);
                if (!empty($blue)) {
                    $user_blue = $userMonitor::get(['user_id' => $user_id, 'type_id' => $blue['id']]);
                    $check_xinlv_data = !empty($user_blue) ? $user_blue : $blue;
                }
                $blue = $userBluetoothModel::get(['pid' => 21]);
                if (!empty($blue)) {
                    $user_blue = $userMonitor::get(['user_id' => $user_id, 'type_id' => $blue['id']]);
                    $check_gao_data = !empty($user_blue) ? $user_blue : $blue;
                }
                $blue = $userBluetoothModel::get(['pid' => 50]);
                if (!empty($blue)) {
                    $user_blue = $userMonitor::get(['user_id' => $user_id, 'type_id' => $blue['id']]);
                    $check_di_data = !empty($user_blue) ? $user_blue : $blue;
                }
                $blue = $userBluetoothModel::get(['pid' => 7]);
                if (!empty($blue)) {
                    $user_blue = $userMonitor::get(['user_id' => $user_id, 'type_id' => $blue['id']]);
                    $check_xueyang_data = !empty($user_blue) ? $user_blue : $blue;
                }
            }

            $state1 = false;
            $state2 = false;
            $oldtime1 = 0;
            $oldtime2 = 0;
            for ($i = 0; $i < 720; $i++) {

                $arr_text[$i]['time'] = date('Y-m-d H:i:s', $timex + (120 * $i));
//                var_dump($num);
//                var_dump($str_arr[$num]);
//                $num += 4;
//                continue;
                if (isset($str_arr[$num])) {
                    $arr_text[$i]['xin'] = base_convert($str_arr[$num], 16, 10);

                    if ($arr_text[$i]['xin'] > 40) {
//                        //心率的总和 和  次数
//                        $xin += $arr_text[$i]['xin'];
//                        $xin_num += 1;
                        //数据每10分钟搞一个
                        $taketime = strtotime($arr_text[$i]['time']);

                        //整点取数
                        if ($taketime % 3600 == 0) {
                            if (($taketime - $oldtime1) % 3600 == 0) {
                                $state1 = true;
                            }

                            $oldtime1 = $taketime;
                        } else {
                            if ($this->is_area($arr_text[$i]['xin'])) {
                                $oldtime1 = $taketime;
                                $state1 = true;
                            }
                        }

                        if ($state1) {
                            $user_info_type_record = InfoTypeRecord::get(['user_id' => $user['id'], 'type_id' => 6, 'day' => $time, 'taketime' => $taketime]);
                            if (!$user_info_type_record) {
                                if (!empty($check_xinlv_data)) {
                                    if ($arr_text[$i]['xin'] > $check_xinlv_data['high'] || $arr_text[$i]['xin'] < $check_xinlv_data['low']) {
                                        $check_xinlv = 1;
                                    }
                                }
                                $info_type_record = new InfoTypeRecord();
                                $info_type_record->save(['user_id' => $user['id'], 'type_id' => 6, 'day' => $time, 'type_value' => $arr_text[$i]['xin'], 'is_use' => 1, 'taketime' => $taketime]);
                            } else {
                                $user_info_type_record->save(['type_value' => $arr_text[$i]['xin']]);
                            }
                            echo 'insert 6' . '-' . $arr_text[$i]['xin'] . '-' . date("Y-m-d H:i", $taketime) . "\n";
                            $state1 = false;
                        } else {
//                            echo 6 . '-' . date("Y-m-d H:i", $taketime) . "\n";
                        }

                    }
//                    log_print('心率：' . $arr_text[$i]['time'] . '->' . $arr_text[$i]['xin'] . '</br>');
                }

                if (isset($str_arr[$num + 1]) && isset($str_arr[$num + 2])) {
                    $arr_text[$i]['shu'] = base_convert($str_arr[$num + 1], 16, 10);
                    $arr_text[$i]['shou'] = base_convert($str_arr[$num + 2], 16, 10);

                    if ($arr_text[$i]['shu'] > 0 && $arr_text[$i]['shou'] > 0) {
//                        //舒张压  收缩压的总和 和  数次  血压  type = 21
//                        $shu += $arr_text[$i]['shu'];
//                        $shou += $arr_text[$i]['shou'];
//                        $shu_num += 1;
                        //数据每10分钟搞一个
                        $user_info_type_record = InfoTypeRecord::get(['user_id' => $user['id'], 'type_id' => 21, 'day' => $time, 'taketime' => strtotime($arr_text[$i]['time'])]);

//                        if ($arr_text[$i]['shou'] < 60 || $arr_text[$i]['shou'] > 190) {
//                            $arr_text[$i]['shou'] = 120;
//                        }
//                        if ($arr_text[$i]['shu'] < 30 || $arr_text[$i]['shu'] > 120) {
//                            $arr_text[$i]['shu'] = 80;
//                        }

                        if ($taketime % 3600 == 0) {
                            if (($taketime - $oldtime2) % 3600 == 0) {
                                $state2 = true;
                            }

                            $oldtime2 = $taketime;

                        } else {
                            if ($this->is_area([$arr_text[$i]['shu'], $arr_text[$i]['shou']], 2)) {
                                $oldtime2 = $taketime;
                                $state2 = true;
                            }
                        }

                        if ($state2) {
                            if (!$user_info_type_record) {
                                if (!empty($check_gao_data)) {
                                    if ($arr_text[$i]['shou'] > $check_gao_data['high'] || $arr_text[$i]['shou'] < $check_gao_data['low']) {
                                        $check_gao = 1;
                                    }
                                }

                                if (!empty($check_di_data)) {
                                    if ($arr_text[$i]['shu'] > $check_di_data['high'] || $arr_text[$i]['shu'] < $check_di_data['low']) {
                                        $check_di = 1;
                                    }
                                }
                                $info_type_record = new InfoTypeRecord();
                                $info_type_record->save(['user_id' => $user['id'], 'type_id' => 21, 'day' => $time, 'type_value' => implode(',', [$arr_text[$i]['shu'], $arr_text[$i]['shou']]), 'is_use' => 1, 'taketime' => $taketime]);
                            } else {
                                $user_info_type_record->save(['type_value' => implode(',', [$arr_text[$i]['shu'], $arr_text[$i]['shou']])]);
                            }

                            echo 'insert 21' . '-' . ($arr_text[$i]['shu'] . ',' . $arr_text[$i]['shou']) . date("Y-m-d H:i", $taketime) . "\n";
                            $state2 = false;
                        } else {
//                            echo 21 . '-' . date("Y-m-d H:i", $taketime) . "\n";
                        }

                        //舒张压   血压低压   血压21
//                        log_print('舒张压：' . $arr_text[$i]['time'] . '->' . $arr_text[$i]['shu'] . '</br>');
                        //收缩压  血压的高压   血压21
//                        log_print('收缩压：' . $arr_text[$i]['time'] . '->' . $arr_text[$i]['shou'] . '</br>');
                    }
                }

                if (isset($str_arr[$num + 3])) {
                    $arr_text[$i]['xue'] = base_convert($str_arr[$num + 3], 16, 10);

                    if ($arr_text[$i]['xue'] > 0) {
//                        //血氧  type  == 7
//                        //心率的总和 和  次数
//                        $xue += $arr_text[$i]['xue'];
//                        $xue_num += 1;
                        //数据每10分钟搞一个
                        $user_info_type_record = InfoTypeRecord::get(['user_id' => $user['id'], 'type_id' => 7, 'day' => $time, 'taketime' => strtotime($arr_text[$i]['time'])]);

//                        if ($arr_text[$i]['xue'] < 55 || $arr_text[$i]['xue'] > 100) {
//                            $arr_text[$i]['shu'] = 95;
//                        }

                        if (!$user_info_type_record) {
                            if (!empty($check_xueyang_data)) {
                                if ($arr_text[$i]['xue'] > $check_xueyang_data['high'] || $arr_text[$i]['xue'] < $check_xueyang_data['low']) {
                                    $check_xueyang = 1;
                                }
                            }
                            $info_type_record = new InfoTypeRecord();
                            $info_type_record->save(['user_id' => $user['id'], 'type_id' => 7, 'day' => $time, 'type_value' => $arr_text[$i]['xue'], 'is_use' => 1, 'taketime' => strtotime($arr_text[$i]['time'])]);
                        } else {
                            $user_info_type_record->save(['type_value' => $arr_text[$i]['xue']]);
                        }
//                        log_print( '血氧：' . $arr_text[$i]['time'] . '->' . $arr_text[$i]['xue'] . '</br>');
                    }
                }

                //因为是四个数据 所以加4
                $num += 4;
            }
            $target = [];
            if ($check_xinlv > 0) {
                $status = Db("user_type_bluetooth")->where("id",9)->value("status");
                if(!status) $target[] = '心率';
            }
            if ($check_gao > 0) {
                $status = Db("user_type_bluetooth")->where("id",10)->value("status");
                if(!status) $target[] = '收缩压';
            }
            if ($check_di > 0) {
                 $status = Db("user_type_bluetooth")->where("id",12)->value("status");
                 if(!status) $target[] = '舒张压';
            }
            if ($check_xueyang > 0) {
                 $status = Db("user_type_bluetooth")->where("id",11)->value("status");
                 if(!status)  $target[] = '血氧';

            }


            if (count($target) > 0 &&  $user['sms_notify'] == 1) {
                $target_str = implode(',', $target);
                log_print($target_str);
                $template['target'] = $target_str;
                Sms::notice($mobile_str, $template, 'SMS_227256586');
            }
        }
    }

    protected function is_area($value, $type = 1)
    {
        //$type = 1 心率，=2 血压
        if ($type == 1) {
            if ($value >= 100 || $value <= 60) return true;
        } else {

            if (($value[0] <= 60 || $value[0] >= 90) ||
                $value[1] <= 90 || $value[1] >= 140
            ) return true;
        }

        return false;
    }
}